package cn.wolfcode.luowowo.redis.redisService.impl;

import cn.wolfcode.luowowo.redis.key.RedisKeys;
import cn.wolfcode.luowowo.redis.redisService.ISecurityRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SecurityRedisServiceImpl implements ISecurityRedisService {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public boolean isAllowBrush(String key) {

        //如果已经存在key 不做任何操作;

        // 如果没有添加一个(相当于使用setex)
        template.opsForValue().setIfAbsent(key, "10",
                RedisKeys.BRUSH_PROOF.getTime(), TimeUnit.SECONDS);
        //执行 使value值减一
        Long decrement = template.opsForValue().decrement(key);
        return decrement >= 0;
    }
}